package org.numb.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.springframework.stereotype.Component;

// 生成一个AuthenticationFilter的bean，被spring托管
@Component
public class AuthenticationFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
        if (!(request instanceof HttpServletRequest)
            || !(response instanceof HttpServletResponse)) {
            throw new ServletException("AuthenticationFilter can handle http requests");
        }
        request.setAttribute("token", "I am root");
        chain.doFilter(request, response);
    }

    @Override
    public void destroy() {

    }
}
